﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;

namespace Interfaces.Tareas
{
    public partial class CrearTarea : System.Web.UI.Page
    {
        Model.Programa programatarea { get { return (Model.Programa)ViewState["programatarea"]; } set { ViewState["programatarea"] = value; } }
        Miembroo directoratarea { get { return (Miembroo)ViewState["directoratarea"]; } set { ViewState["directoratarea"] = value; } }
        Model.Tareas.TareaCampos tareascampos { get { return (Model.Tareas.TareaCampos)ViewState["tareacampos"]; } set { ViewState["tareacampos"] = value; } }
        public string ci;//"123";       
        Model.Tareas.Tarea tarea;
        protected void Page_Load(object sender, EventArgs e)
        {
            ci=Convert.ToString( DataBase.Miembro2.BDMiembro.DevolverMiembroPorUsuario(User.Identity.Name).CI);
            if(!IsPostBack)
                tareascampos = new Model.Tareas.TareaCampos();
            tarea = new Model.Tareas.Tarea();
            if (Session["programatarea"] != null)
            {
                //CheckBox1.Checked = true;
                TextBox6.Enabled = true;
                programatarea = (Model.Programa)Session["programatarea"];
                TextBox6.Text = programatarea.Nombre;
                //Session.Remove("programatarea");
            }
            if (Session["directoratarea"] != null)
            {
                directoratarea = (Miembroo)Session["directoratarea"];
                TextBox5.Text = directoratarea.Nombre + " " + directoratarea.ApellidoPaterno;              
            }
            if (Session["tareacampos"] != null)
            {
                tareascampos=(Model.Tareas.TareaCampos)Session["tareacampos"];
                TextBox1.Text = tareascampos.nombre;
                TextBox2.Text = tareascampos.descripcion;
                if (tareascampos.fecini != default(DateTime))
                    TextBox3.Text =Convert.ToString( tareascampos.fecini.Year ) +"-"+Convert.ToString( tareascampos.fecini.Month) +"-"+ Convert.ToString(tareascampos.fecini.Day);//Convert.ToString(tareascampos.fecini.ToShortDateString());
                else
                    TextBox3.Text = "";
                if (tareascampos.fecfin != default(DateTime))
                    TextBox4.Text = Convert.ToString(tareascampos.fecini.Year) + "-" + Convert.ToString(tareascampos.fecfin.Month) + "-" + Convert.ToString(tareascampos.fecfin.Day);//Convert.ToString(tareascampos.fecfin.ToShortDateString());
                 else
                    TextBox4.Text = "";
                Session.Remove("tareacampos");                  
            }
            if (!IsPostBack)
            {
               TextBox6.Enabled = true;
                MultiView1.ActiveViewIndex = 0;
           //     DataList1.DataSource = DataBase.Miembro2.BDMiembro.DevolverTodos();
             //   DataList1.DataBind();               
            }           
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            bool resp = false;
            Label3.Visible = false;       
                try
                {           
                    if ((TextBox1.Text == "" ) || (TextBox2.Text=="") || (TextBox3.Text=="" )|| (TextBox4.Text=="") || (TextBox5.Text=="") )
                        throw new Exception("*Faltan Campos por llenar");                    
                    tarea.ci = ci;
                    tarea.ciAsignado = Convert.ToString( directoratarea.CI);//Label1.Text;
                    tarea.fecini =DateTime.Parse(TextBox3.Text + " 0:0:0");
                    tarea.fecfin =DateTime.Parse(TextBox4.Text + " 0:0:0");
                    if (tarea.fecini.Year < 1967)
                        throw new Exception("*EL año de la fecha inicial debe ser mayor a 1967 "); 
                    if(tarea.fecini>tarea.fecfin)
                        throw new Exception("*La fecha inicial no puede ser mayor a la fecha final");     
                    tarea.porcentaje = 0;
                    tarea.estado = false;
                    tarea.nombre = TextBox1.Text;
                    tarea.descripcion = TextBox2.Text;
                 
                    if (TextBox6.Text!="")
                    {
                        tarea.programa =Convert.ToString( programatarea.CodigoPrograma);
                        resp = Processes.Tareas.GestorTareas.CrearTareaPrograma(tarea);
                    }
                    else
                    {
                        resp = Processes.Tareas.GestorTareas.CrearTarea(tarea);
                    }
                    if (resp == true)
                    {                                               
                        TextBox1.Text = "" ;
                        TextBox2.Text="";
                        TextBox3.Text="";
                        TextBox4.Text="";
                        TextBox5.Text = "";
                        TextBox6.Text = "";
                        Label3.Visible = false;
                        //CheckBox1.Checked = false;
                        Session.Remove("tareacampos");
                        Session.Remove("directoratarea");
                        Session.Remove("programatarea");
                    }
                }
                catch (Exception ex)
                {
                    Label3.Text = ex.Message;
                    Label3.Visible = true;
                }                    
        }

       /* protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox1.Checked == true)
            {
                //int ges = 2012;
              //  DataList2.DataSource = Processes.Tareas.GestorTareas.DevolversusProgramas(ci,ges);//DataBase.Programa.BDPrograma.DevolverTodosLosProgramas();
               // DataList2.DataBind();
                TextBox6.Enabled = true;
                //MultiView1.ActiveViewIndex = 2;
                if (TextBox1.Text != "")
                    tareascampos.nombre = TextBox1.Text;
                if (TextBox2.Text != "")
                    tareascampos.descripcion = TextBox2.Text;
                if (TextBox3.Text != "")
                    tareascampos.fecini = Convert.ToDateTime(TextBox3.Text);
                if (TextBox4.Text != "")
                    tareascampos.fecfin = Convert.ToDateTime(TextBox4.Text);
                Session["tareacampos"] = tareascampos;
                Response.Redirect("/Programa/BuscarPrograma.aspx?Index=" + true + "&Verificar=" + true);
            }
            else
            {
                TextBox6.Enabled = false;
                TextBox6.Text = "";
            }
        }*/

        protected void MiembroBtn_Click(object sender, EventArgs e)
        {
            LinkButton b = sender as LinkButton;
            Label1.Text=  Convert.ToString( b.CommandArgument);
            MultiView1.ActiveViewIndex = 0;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {                       
            if(TextBox1.Text!="")
                tareascampos.nombre=TextBox1.Text ;
            if (TextBox2.Text != "")
                tareascampos.descripcion=TextBox2.Text ;
            if (TextBox3.Text != "")
                tareascampos.fecini = Convert.ToDateTime( TextBox3.Text);
            if (TextBox4.Text != "") 
                tareascampos.fecfin = Convert.ToDateTime(TextBox4.Text);
            Session["tareacampos"] = tareascampos;
            Response.Redirect("/Miembro/BuscarMiembro.aspx?Index=" + false+"&VerificarPagina=0");
            //Response.Redirect("/Miembro/BuscarMiembro.aspx?Index=1 &Verificar=" + true);            
        }

        protected void ProgramaBtn_Click(object sender, EventArgs e)
        {
            LinkButton b = sender as LinkButton;    
            Label2.Text = Convert.ToString(b.CommandArgument);
            MultiView1.ActiveViewIndex = 0;
        }

        protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
        {
            Label nom = (Label)e.Item.FindControl("Nombre");           
            TextBox6.Text = nom.Text;
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            Label nombre = (Label)e.Item.FindControl("Nombre");
            TextBox5.Text = nombre.Text;
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {


            if (Session["BuscarMaster"] != null)
            {
                this.MasterPageFile = (string)Session["BuscarMaster"];
            }
            else
            {
                this.MasterPageFile = "~/MasterPages/Default.Master";
            }
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            //int ges = 2012;
            //  DataList2.DataSource = Processes.Tareas.GestorTareas.DevolversusProgramas(ci,ges);//DataBase.Programa.BDPrograma.DevolverTodosLosProgramas();
            // DataList2.DataBind();
            TextBox6.Enabled = true;
            //MultiView1.ActiveViewIndex = 2;
            if (TextBox1.Text != "")
                tareascampos.nombre = TextBox1.Text;
            if (TextBox2.Text != "")
                tareascampos.descripcion = TextBox2.Text;
            if (TextBox3.Text != "")
                tareascampos.fecini = Convert.ToDateTime(TextBox3.Text);
            if (TextBox4.Text != "")
                tareascampos.fecfin = Convert.ToDateTime(TextBox4.Text);
            Session["tareacampos"] = tareascampos;
            //Response.Redirect("/Programa/BuscarPrograma.aspx?Index=" + true + "&Verificar=" + true);
            Response.Redirect("/Programa/BuscarPrograma.aspx?Index=1 &Verificar=0");
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            TextBox6.Text = "";
            Session.Remove("programatarea");
        }
    }
}